package com.gaea.tools.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.gaea.tools.entity.W5CoreTableColumn;
import com.gaea.tools.entity.W5TableDataColumn;
import com.gaea.tools.mapper.W5TableDataMapper;
import com.gaea.tools.service.IJeCoreResourcetableService;
import com.gaea.tools.service.IW5CoreTablecolumnService;
import com.gaea.tools.service.IW5TablecolumnDataService;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 资源表-->列管理 前端控制器
 * </p>
 *
 * @author erma
 * @since 2022-01-24
 */
@RestController
@RequestMapping("/mdm/w5-core-tablecolumn")
public class W5CoreTablecolumnController {

    @Autowired
    private IW5CoreTablecolumnService  iW5CoreTablecolumnService;

    @Autowired
    private com.gaea.tools.mapper.W5TableDataMapper W5TableDataMapper;

    @Autowired
    private IW5TablecolumnDataService iW5TablecolumnDataService;

    @Autowired
    private IJeCoreResourcetableService iJeCoreResourcetableService;

    /**
     * 更新资源表列管理符合条件的字段去除je/JE化
     * 接口地址：http://localhost:8081/mdm/w5-core-tablecolumn/w5upTableCodels
     * @return
     */
    @RequestMapping("/w5upTableCodels")
    public String upTableCodels(){

        List<String> updateErroTable = new ArrayList<>();

        /**
         * 1.获取资源表列管理需要修改的表
         */
        QueryWrapper<W5CoreTableColumn> jeCoreTablecolumnQueryWrapper = new QueryWrapper<W5CoreTableColumn>();
        jeCoreTablecolumnQueryWrapper
                .or(wq->wq.like("TABLECOLUMN_CODE","CLS")
                        .or().like("TABLECOLUMN_CODE","ICON"));
        jeCoreTablecolumnQueryWrapper.groupBy("TABLECOLUMN_TABLECODE");
        List<W5CoreTableColumn> tableList = iW5CoreTablecolumnService.list(jeCoreTablecolumnQueryWrapper);

        for (W5CoreTableColumn table : tableList) {

            /**需要更改的表编码*/
            String  tableCode=table.getTablecolumntablecode();

            /**
             * 2.根据表编码查询资源表列管理获取需要修改的列
             */
            QueryWrapper<W5TableDataColumn> W5TableDataColumnQueryWrapper = new QueryWrapper<W5TableDataColumn>();
            W5TableDataColumnQueryWrapper
                    .or(wq->wq.like("TABLECOLUMN_CODE","CLS")
                            .or().like("TABLECOLUMN_CODE","ICON")).eq("TABLECOLUMN_TABLECODE",tableCode);
            List<W5TableDataColumn> tableColumnDatas = iW5TablecolumnDataService.list(W5TableDataColumnQueryWrapper);

            //System.out.println("------------------列编码-------------------------");
            /**每个表需要修改的列属性集合*/
            ArrayList<String>  tableColumns=new ArrayList<String>();
            if(tableColumnDatas.size()>0){
                //System.out.println("循环出来的表名：W5_SYS_FILETYPE");
                //String columneTableId="W5_CORE_FONTICON_ID";

                System.out.println("循环出来的表名："+tableCode);
                String columneTableId=tableCode+"_ID";
                System.out.println("表主键："+columneTableId);
                for (W5TableDataColumn Columndata : tableColumnDatas) {

                    if(!columneTableId.equals(Columndata.getTablecolumnCode())){
                        //String  updateje="update W5_SYS_FILETYPE set ";
                        //String  updateJE="update W5_SYS_FILETYPE set ";

                        /**
                         * 3.获取每个表需要更新字段的sql
                         * 去je和去JE化
                         */
                        String  updateje="update "+tableCode+" set ";
                        String  updateJE="update "+tableCode+" set ";
                        //System.out.println("每次循环输出列:"+Columndata.getTablecolumnCode());
                        tableColumns.add(Columndata.getTablecolumnCode());
                        updateje=updateje+Columndata.getTablecolumnCode()+"=REPLACE ("+Columndata.getTablecolumnCode()+",'"+"je"+"','"+"w5"+"')"+" where "+Columndata.getTablecolumnCode()+" like '%je%'";
                        updateJE=updateJE+Columndata.getTablecolumnCode()+"=REPLACE ("+Columndata.getTablecolumnCode()+",'"+"JE"+"','"+"W5"+"')"+" where "+Columndata.getTablecolumnCode()+" like '%JE%'";
                        System.out.println("每次修改列的sqlje:-->"+updateje);
                        System.out.println("每次修改列的sqlJE:-->"+updateJE);

                        /**
                         *4.执行update操作更新每个表的数据去je/JE化
                         */
                        try {
                            iJeCoreResourcetableService.executeUpdate(updateje);
                            iJeCoreResourcetableService.executeUpdate(updateJE);
                        } catch (Exception e) {
                            System.out.println("------------------------------------------------" + tableCode);
                            updateErroTable.add(tableCode);
                            System.out.println(e.getMessage());
                        }
                    }
                }
            }else{
                System.out.println("未查询出"+tableCode+"表对象的数据");
            }
            System.out.println("每个表的列编码"+tableColumns);

            /**无用逻辑代码-->*/
            /**
            System.out.println("-----------------每个表的数据--------------------------");

            List<Map<String, String>> listdata= W5TableDataMapper.getTableInfo("W5_CORE_RESOURCEBUTTON");

            for (Map data : listdata) {
                System.out.println(data.get(arr.get(0)));

               if(data.get(arr.get(0))==null){
                   System.out.println("---------------->");

               }else {
                   try {
                       //String  updateStringValue=data.get(arr.get(0)).toString();
                       String  updateStringValue="je_QWE_je_QEQWEQ";
                      // System.out.println("字符春的值"+updateStringValue);
                       if(updateStringValue.isEmpty()){
                           System.out.println("11111111111111111111111--->");
                       }else {
                           if(updateStringValue.contains("JE")){
                               System.out.println("包含JE------------------>"+updateStringValue);
                               String s = updateStringValue.replaceAll("JE","W5");
                               System.out.println("替换后的字串"+s);

                           }else if(updateStringValue.contains("je")){
                               System.out.println("包含je------------------>"+updateStringValue);
                               String ss = updateStringValue.replaceAll("je","w5");
                               System.out.println("替换后的字串"+ss);
                           }

                           //UPDATE table_name SET field1=new-value1, field2=new-value2
                           //[WHERE Clause]
                       }
                   }catch (Exception e){
                       System.out.println(e);
                   }
               }
            }
             */
        }

        return "更新成功";
        //return JSON.toJSONString(updateErroTable);
    }
}
